package com.niiwoo.civet.trade.report.ifcert.niwo;

import com.niiwoo.civet.trade.property.IfcertProperties;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class SendClient {

    public String sendPost(JSONObject json, Long timestamp, String url,String nonce,IfcertProperties ifcertProperties) {
        //String nonce = Integer.toHexString(new Random().nextInt());
        String token = getApiKey(ifcertProperties.getApikey(), ifcertProperties.getSourceCode(), ifcertProperties.getVersion(), timestamp, nonce);
        String msg = json.toString();
        Map<String, String> params = new HashMap<>(2);
        params.put("apiKey", token);
        params.put("msg", msg);
        return SendUtils.postRequest(url, params);
    }



    /**
     * apiKey加密
     *
     * @param apiKey
     * @param source_code
     *            平台编号
     * @param versionStr
     *            版本号，如：1.3 ; 1.4
     * @param currentTime
     *            时间戳
     * @param nonce
     *            随机数
     * @return
     */
    private String getApiKey(String apiKey, String source_code, String versionStr, Long currentTime,
                                    String nonce) {
        double version_double = Double.valueOf(versionStr);
        int version = (int) (version_double * 100);
        String versionHex = "0x" + Integer.toHexString(version);
        String s = source_code +
                versionHex +
                apiKey +
                currentTime +
                nonce;
        //return SHA.SHA256(s);
        return s;
    }
}
